package com.athena.crm.controller;

import java.util.List;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import com.athena.crm.domain.bo.CrmCluePoolToClueBo;
import com.athena.crm.domain.bo.CrmCustomerToHighSeasBo;
import com.athena.crm.domain.vo.CrmClueVo;
import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.athena.common.annotation.RepeatSubmit;
import com.athena.common.annotation.Log;
import com.athena.common.core.controller.BaseController;
import com.athena.common.core.domain.PageQuery;
import com.athena.common.core.domain.R;
import com.athena.common.core.validate.AddGroup;
import com.athena.common.core.validate.EditGroup;
import com.athena.common.core.validate.QueryGroup;
import com.athena.common.enums.BusinessType;
import com.athena.common.utils.poi.ExcelUtil;
import com.athena.crm.domain.vo.CrmCluePoolVo;
import com.athena.crm.domain.bo.CrmCluePoolBo;
import com.athena.crm.service.ICrmCluePoolService;
import com.athena.common.core.page.TableDataInfo;

/**
 * CRM线索池
 *
 * @author ruoyi
 * @date 2023-09-10
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/crm/cluePool")
public class CrmCluePoolController extends BaseController {

    private final ICrmCluePoolService iCrmCluePoolService;

    /**
     * 查询CRM线索池列表
     */
    @SaCheckPermission("crm:cluePool:list")
    @GetMapping("/list")
    public TableDataInfo<CrmCluePoolVo> list(CrmCluePoolBo bo, PageQuery pageQuery) {
        return iCrmCluePoolService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出CRM线索池列表
     */
    @SaCheckPermission("crm:cluePool:export")
    @Log(title = "CRM线索池", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(CrmCluePoolBo bo, HttpServletResponse response) {
        List<CrmCluePoolVo> list = iCrmCluePoolService.queryList(bo);
        ExcelUtil.exportExcel(list, "CRM线索池", CrmCluePoolVo.class, response);
    }

    /**
     * 获取CRM线索池详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("crm:cluePool:query")
    @GetMapping("/{id}")
    public R<CrmCluePoolVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(iCrmCluePoolService.queryById(id));
    }

    /**
     * 新增CRM线索池
     */
    @SaCheckPermission("crm:cluePool:add")
    @Log(title = "CRM线索池", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody CrmCluePoolBo bo) {
        return toAjax(iCrmCluePoolService.insertByBo(bo));
    }

    /**
     * 修改CRM线索池
     */
    @SaCheckPermission("crm:cluePool:edit")
    @Log(title = "CRM线索池", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody CrmCluePoolBo bo) {
        return toAjax(iCrmCluePoolService.updateByBo(bo));
    }

    /**
     * 删除CRM线索池
     *
     * @param ids 主键串
     */
    @SaCheckPermission("crm:cluePool:remove")
    @Log(title = "CRM线索池", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iCrmCluePoolService.deleteWithValidByIds(Arrays.asList(ids), true));
    }

    /**
     * 跟进条件获取CRM客户信息
     *
     * @param name 姓名
     * @param phone 电话
     * @return
     */
    @SaCheckPermission("crm:cluePool:query")
    @GetMapping("/{name}/{phone}")
    public R<CrmCluePoolVo> getInfoByNameAndPhone(@NotNull(message = "姓名不能为空") @PathVariable String name,
                                              @NotNull(message = "电话不能为空") @PathVariable String phone) {
        return R.ok(iCrmCluePoolService.queryByNameAndPhone(name, phone));
    }

    /**
     * 线索池转线索
     */
    @SaCheckPermission("crm:cluePool:rob")
    @Log(title = "客户公海转客户", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PostMapping("/transferCluePoolToClue")
    public R<Void> transferCustomerHighSeasToCustomer(@Validated(AddGroup.class) @RequestBody CrmCluePoolToClueBo bbo) {
        return toAjax(iCrmCluePoolService.cluePooltoClue(bbo));
    }
}
